package com.lagou.fastdfs.service;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
/**
 * @ClassName FastDFSClientService
 * @Description TODO
 * @Author xsq
 * @Date 2020/9/19 15:16
 **/
@Component
public class FastDFSClientService {
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
    /**
     * 文件上传
     *
     * @param bytes 文件字节
     * @param fileSize 文件大小
     * @param extension 文件扩展名
     * @return fastDfs路径
     */
    public String uploadFile(byte[] bytes, long fileSize, String extension) {
        ByteArrayInputStream byteArrayInputStream = new
                ByteArrayInputStream(bytes);
        StorePath storePath =
                fastFileStorageClient.uploadFile(byteArrayInputStream,
                        fileSize, extension, null);
        System.out.println(storePath.getGroup() + ":" + storePath.getPath() +
                ":"
                + storePath.getFullPath());
        return storePath.getFullPath();
    }
    /**
     * 下载文件
     *
     * @param fileUrl 文件URL
     * @return 文件字节
     * @throws IOException
     */
    public byte[] downloadFile(String fileUrl) throws IOException {
        if(fileUrl.startsWith("/")){
            fileUrl = fileUrl.substring(1);
        }
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        DownloadByteArray downloadByteArray = new DownloadByteArray();
        byte[] bytes = fastFileStorageClient.downloadFile(group, path,
                downloadByteArray);
        return bytes;
    }
}
